package com.ma.web.admin;

import com.ma.po.Tag;
import com.ma.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    TagService tagsService;
    //标签页的分页显示，标签页table页的跳转
    @GetMapping("/tags")
    public String tags(@PageableDefault(size=7
            ,sort={"id"}
            ,direction = Sort.Direction.DESC)
                                   Pageable pageable, Model model){

        model.addAttribute("page",tagsService.ListTag(pageable));
        //page是json格式
        return "admin/tags";
    }

    //跳转新增标签页
    @GetMapping("/tags/input")
    public String input(Model model){
        //为了防止thyemeleaf模板出错，添加一个空的tag对象
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    //新增标签页的提交
    //BindingResult向tags-input页面传递错误信息
    //RedirectAttributes向跳转的页面传递错误信息，即向tags页面
    @PostMapping("/tags")
    public String post(@Valid Tag tag, BindingResult result
            , RedirectAttributes  redirectAttributes){
        // 1.判断是否有重复的tag
        Tag t=tagsService.getTagByName(tag.getName());
        if(t!=null){
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        if(result.hasErrors()){
            return "admin/tags-input";
        }
         //如果没有相同的tag，则进行保存，并输出提示信息
        Tag t1=tagsService.saveTag(tag);
        if(t1 != null)
            redirectAttributes.addFlashAttribute("message","添加成功");
        else
            redirectAttributes.addFlashAttribute("message","添加失败");
        return "redirect:/admin/tags";
    }
    //跳转编辑标签页
    @GetMapping("/tags/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("tag",tagsService.getTag(id));
        return "admin/tags-input";

    }

    //标签页的编辑提交
    @PostMapping("/tags/{id}")
    public String editPost(@Valid Tag tag, BindingResult result
            , RedirectAttributes  redirectAttributes,@PathVariable Long id){
        //1.判断是否有重复的tag
        Tag t=tagsService.getTagByName(tag.getName());
        if(t!=null){
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        if(result.hasErrors()){
            return "admin/tags-input";
        }
        //如果没有相同的tag，则进行更新，并输出提示信息
        Tag t1=tagsService.updateTag(id,tag);
        if(t1 != null)
            redirectAttributes.addFlashAttribute("message","修改成功");
        else
            redirectAttributes.addFlashAttribute("message","修改失败");
        return "redirect:/admin/tags";
}
    //    删除标签
    @GetMapping("/tags/{id}/delete")
    public  String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
        tagsService.deleteTag(id);
        redirectAttributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";

}

}
